<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>导线统计</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .layui-form-label { width: 100px; }
        .layui-input-inline { width: 200px; }
        .layui-table-cell { text-align: center; }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">导线名称</label>
                            <div class="layui-input-inline">
                                <select id="name" lay-filter="name">
                                    <option value="">请选择导线名称</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始日期</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="startDate" placeholder="请选择开始日期">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束日期</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="endDate" placeholder="请选择结束日期">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"
                                    lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 查询
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="wireStatisticsTable" lay-filter="wireStatisticsFilter"></table>

        <script type="text/html" id="tableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="export">导出Excel</button>
            </div>
        </script>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate,
            util = layui.util;

        // 日期
        laydate.render({ elem: '#startDate', type: 'date' });
        laydate.render({ elem: '#endDate',   type: 'date' });

        // 下拉框：导线名称
        function loadWireNames() {
            $.get('/statistics/wire/names', function (res) {
                var html = '<option value="">请选择导线名称</option>';
                res.forEach(function (n) {
                    html += '<option value="' + n + '">' + n + '</option>';
                });
                $('#name').html(html);
                form.render('select');
            });
        }
        loadWireNames();

        // 表格
        table.render({
            elem: '#wireStatisticsTable',
            url: '/statistics/wire/monthly',
            loading: true,
            toolbar: '#tableToolbar',
            parseData: function (res) {
                return { code: 0, msg: '', count: res.length, data: res };
            },
            <!-- 修改表格列定义部分 -->
            <!-- 修改表格列定义部分 -->
            cols: [[
                {field: 'name', title: '导线名称', width: 150, align: 'center'},
                {field: 'materialCode', title: '导线编号', width: 150, align: 'center'},
                {field: 'specifications', title: '导线规格', width: 180, align: 'center', sort: true},
                {field: 'feedbackDate', title: '更新时间', width: 180, align: 'center',
                    templet: function (d) {
                        return d.feedbackDate ? util.toDateString(d.feedbackDate, 'yyyy-MM-dd HH:mm:ss') : '';
                    }
                },
                {field: 'source', title: '供应商id', width: 100, align: 'center'},
                {field: 'remark', title: '备注', minWidth: 200, align: 'center'},
                {field: 'unit', title: '单位', width: 80, align: 'center'}
            ]],
            page: true,
            skin: 'line',
            limit: 10,
            limits: [10, 20, 50]
        });

        // 搜索
        form.on('submit(data-search-btn)', function () {
            var name      = $('#name').val();
            var startDate = $('#startDate').val();
            var endDate   = $('#endDate').val();

            if (startDate && endDate && startDate > endDate) {
                layer.msg('开始日期不能晚于结束日期', {icon: 2});
                return false;
            }

            table.reload('wireStatisticsTable', {
                where: { name: name, startDate: startDate, endDate: endDate },
                page: { curr: 1 }
            });
            return false;
        });

        // 导出
        table.on('toolbar(wireStatisticsFilter)', function (obj) {
            if (obj.event === 'export') {
                var name      = $('#name').val();
                var startDate = $('#startDate').val();
                var endDate   = $('#endDate').val();

                var url = '/statistics/wire/export?' +
                    'name=' + encodeURIComponent(name || '') +
                    '&startDate=' + encodeURIComponent(startDate || '') +
                    '&endDate=' + encodeURIComponent(endDate || '');
                window.location.href = url;
            }
        });
    });
</script>
</body>
</html>